/* Copyright 2011 Elektrobit Wireless Communications Oy (EB)


  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License version 2 as 
  published by the Free Software Foundation.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License along
  with this program; if not, write to the Free Software Foundation, Inc.,
  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


 *All rights reserved 
 *Version:Elektrobit_Qt_1.0.0_20110305 */


import Qt 4.7
import "qmlItem" 1.0

import Qt 4.7

Item {
    id: mainFrame
    width: 640
    height: 360

    property int photoListIndex: 0
    property real xDurationChange: 1 //to speed up or down when draging speed bar


    function photoCreator() {
        var photoComponent = Qt.createComponent("./qmlItem/PhotoRiverImageDelegate.qml");
        var photoObject = photoComponent.createObject(mainFrame);

        function getLayerNum() {
            var randomNum = Math.random();

            if (randomNum < 0.33) {return -1}
            else if (randomNum < 0.66) {return 0}
            else {return 1}
        }

        if (photoComponent.status == Component.Ready) {
            photoObject.source = imageDatas[photoListIndex].indexImage;
            photoObject.y = Math.random() * (mainFrame.height - photoObject.height);

            //firstly define photo scale value, other property values
            //should be based on scale value. Otherwise scale-z-opacity-xDuration
            //relations will disorder...
            photoObject.scale = 1.0 + 0.25 * getLayerNum(); // possible values: 0.75, 1, 1.25
            photoObject.z = 3 + (photoObject.scale - 1) / 0.25; // possible values: 2, 3, 4

            {photoObject.xDuration =
                (6000 - 2000 * (photoObject.scale - 1) / 0.25 + // basic speed
                getLayerNum() * 500) // slight speed difference of photos in the same layer
            }
        }
    }

    Timer {
        id: pRInterval
        interval: 700
        running: true
        repeat: true
        triggeredOnStart: true

        onTriggered: {

            photoCreator();

            if (photoListIndex < imageDatasCount() - 1) {
                photoListIndex ++;
            } else {
                photoListIndex = 0;
            }
        }
    }

//    Text {
//        id: photoIndexText
//        x: 15
//        anchors.verticalCenter: speedBar.verticalCenter
//        z: 4.5
//        color: "yellow"
//        text: "PHOTO  " + (photoListIndex + 1) + " / " + imageDatasCount()
//        opacity: speedBarText.opacity
//        smooth: true
//        font.pointSize: speedBarText.font.pointSize
//        font.weight: speedBarText.font.weight
//    }

    Text {
        id: speedBarText
        x: 380
        y: 15
        anchors.verticalCenter: speedBar.verticalCenter
        z: 4.5
        color: "yellow"
        text: "SPEED"
        opacity: 0.5
        smooth: true
        font.pixelSize: 16
        font.weight: Font.Bold
    }

    Rectangle {
        id: speedBar
        width: 160; height: 32
        anchors.left: speedBarText.right
        anchors.leftMargin: 20
        y: 15
        color: "skyblue"
        smooth: true
        opacity: 0.7
        radius: 16
        z: 4.5


        Image {
            id: speedButton
            source: "../../cn/images/sys/speedButtonBig.png"
            width: 64; height: 64
            x: speedBar.width / 2 - width / 2 //80 - 32
            anchors.verticalCenter: parent.verticalCenter
            smooth: true
            opacity: 0.9
            z: speedBar.z + 0.1

            MouseArea {
                id: pRMouseArea
                anchors.fill: parent
                clip: true

                drag.target: parent
                drag.axis: Drag.XAxis
                drag.minimumX: - parent.width / 2 + 16
                drag.maximumX: speedBar.width - parent.width / 2 - 16

                onPositionChanged: {
                    mainFrame.xDurationChange = (131 - speedButton.x) / 65
                    pRInterval.interval = 700 * ((131 - speedButton.x) / 65)

                }
            }
        }

    }


    BackButton {
        id:backButton
        x:560
        y:310
        width: 80
        height: 50
        //icon: "../images/sys/button_exit.png"
        z:4.5
        buttonText: "back"
    }

    Connections{
        target: backButton
        onBackButtonClicked:{
            onIconClick("return");
        }
    }


    Rectangle { //the background
        id: pRBackground
        anchors.fill: parent
        color: "black"
    }

    Rectangle { //layer1
        id: pRLayer1
        anchors.fill: parent
        z: 2.5
        color: "black"
        opacity: 0.5

        Behavior on opacity {
            NumberAnimation {id: opacityAnim1; duration: 400}
        }
    }

    Rectangle { //layer2
        id: pRLayer2
        anchors.fill: parent
        z: 3.5
        color: "black"
        opacity: 0.4

        Behavior on opacity {
            NumberAnimation {id: opacityAnim2; duration: 400}
        }
    }

    Rectangle { //the top-most layer, to perform background light
                //change when the focused image is enlarged
        id: pRLayer3
        anchors.fill: parent
        z: 5
        color: "black"
        opacity: 0

        Behavior on opacity {
            NumberAnimation {id: layer3OpacityAnim; duration: 400}
        }

        MouseArea { // to cover the screen so that other MouseAreas can not be
                    // touched when the focused image is enlarged
            id: pRMouseCover
            anchors.fill: parent
            enabled: false
        }
    }

}
